/**
 * 
 */
package by.grsu.coctails.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import by.grsu.coctails.common.utils.GetSystemService;

import com.google.android.imageloader.ImageLoader;

/**
 * Class use for {@link AdapterView}, contains common logic for working with
 * items.
 * 
 * @author Uladzimir_Klyshevich
 * 
 * @param <T>
 *            class of list item
 */
public abstract class AbstractAdapter<T> extends ArrayAdapter<T> {

	private static final int MESSAGE_CODE = 888;

	/** The Constant TAG. */
	private static final String TAG = AbstractAdapter.class.getSimpleName();

	/* Application context. */
	private final Context mContext;

	/* List items. */
	private final List<T> mList;

	/* Layout resource for draw one item. */
	private final int mItemResource;

	private int currentPosition = 0;

	private ImageLoader mImageLoader;

	// private Paginator paginator;
	//
	// public Paginator getPaginator() {
	// return paginator;
	// }
	//
	// public void setPaginator(Paginator paginator) {
	// this.paginator = paginator;
	// }

	/**
	 * Default constructor.
	 * 
	 * @param c
	 *            context
	 * @param pItemResource
	 *            layout resources for draw one item
	 * @param pList
	 *            list items
	 */
	public AbstractAdapter(Context c, int pItemResource, List<T> pList) {
		super(c, pItemResource, 0, pList);
		mList = pList;
		mContext = c;
		mItemResource = pItemResource;
		mImageLoader = (ImageLoader) GetSystemService.get(c,
				ImageLoader.IMAGE_LOADER_SERVICE);
	}

	/*
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		if (mList == null)
			return 0;
		return mList.size();
	}

	/*
	 * @see android.widget.Adapter#getItem(int)
	 */
	public T getItem(int position) {
		return mList.get(position);
	}

	/*
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return Long.valueOf(position);
	}

	private long lastGetViewTime = System.currentTimeMillis();

	/*
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		lastGetViewTime = System.currentTimeMillis();
		if (convertView == null) {
			convertView = createView();
		}
		final T item = getItem(position);
		init(convertView, item);
		return convertView;
	}
	
	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	/**
	 * Initialize view from item data.
	 * 
	 * @param convertView
	 *            view
	 * @param item
	 *            item from list
	 */
	public abstract void init(View convertView, T item);

	/**
	 * Inflate view from resource.
	 * 
	 * @return view
	 */
	public View createView() {
		return View.inflate(mContext, mItemResource, null);
	}

	public Context getContext() {
		return mContext;
	}

	public List<T> getList() {
		return mList;
	}

	public int getCurrentPosition() {
		return currentPosition;
	}

	public void setCurrentPosition(int currentPosition) {
		this.currentPosition = currentPosition;
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == MESSAGE_CODE) {
				notifyDataSetChanged();
			} else {
				super.handleMessage(msg);
			}
		}

	};

	private int prevCount = -1;

	@Override
	public void notifyDataSetChanged() {
		handler.removeMessages(MESSAGE_CODE);
		if (prevCount == getCount()) {
			long dim = System.currentTimeMillis() - lastGetViewTime;
			if (dim > 300l) {
				super.notifyDataSetChanged();
			} else {
				handler.sendEmptyMessageDelayed(MESSAGE_CODE, dim + 1);
			}
		} else {
			prevCount = getCount();
			super.notifyDataSetChanged();
		}
	}

}
